package com.zmn.mcc.services.impl.permit;

import com.zmn.common.constant.GlobalConsts;
import com.zmn.common.dictionary.GlobalDict;
import com.zmn.common.dto.StatusDTO;
import com.zmn.common.utils.date.DateUtil;
import com.zmn.common.utils.number.NumberUtil;
import com.zmn.mcc.common.dto.permit.PermitLitDO;
import com.zmn.mcc.model.entity.permit.McPermit;
import com.zmn.mcc.model.entity.permit.McPermitQuery;
import com.zmn.mcc.persistence.interfaces.permit.PermitDao;
import com.zmn.mcc.services.interfaces.permit.PermitService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @author heguanghua
 * @since 2020/04/14 14:50
 */
@Service
public class PermitServiceImpl implements PermitService {

    @Resource
    private PermitDao permitDao;

    @Override
    public McPermit findByKey(Integer permitId) {
        return permitDao.findByKey(permitId);
    }

    @Override
    public List<McPermit> listPageByQuery(McPermitQuery query) {
        Integer count = this.countByQuery(query);
        query.setTotalCount(count);
        return count == 0 ? new ArrayList<>(0) : permitDao.listPageByQuery(query);
    }

    @Override
    public List<McPermit> listAll() {
        return this.listAll(null);
    }

    @Override
    public List<McPermit> listAll(Integer status) {
        McPermitQuery query = new McPermitQuery();
        query.setStatus(status);
        return this.listByQuery(query);
    }

    @Override
    public List<McPermit> listBySysId(Integer sysId) {
        return this.listBySysId(sysId, null);
    }

    @Override
    public List<McPermit> listBySysId(Integer sysId, Integer status) {
        McPermitQuery query = new McPermitQuery();
        query.setSysId(sysId);
        query.setStatus(status);
        return this.listByQuery(query);
    }

    @Override
    public List<McPermit> listByModuleId(Integer moduleId) {
        return this.listByModuleId(moduleId, null);
    }

    @Override
    public List<McPermit> listByModuleId(Integer moduleId, Integer status) {
        McPermitQuery query = new McPermitQuery();
        query.setModuleId(moduleId);
        query.setStatus(status);
        return this.listByQuery(query);
    }

    @Override
    public List<McPermit> listByParentId(Integer parentId) {
        return this.listByParentId(parentId, null);
    }

    @Override
    public List<McPermit> listByParentId(Integer parentId, Integer status) {
        McPermitQuery query = new McPermitQuery();
        query.setParentId(parentId);
        query.setStatus(status);
        return this.listByQuery(query);
    }

    @Override
    public List<McPermit> listByQuery(McPermitQuery query) {
        return permitDao.listByQuery(query);
    }

    @Override
    public List<PermitLitDO> listPermitLitBySysIdAndRoleIds(List<Integer> roleIds, Integer sysId) {
        McPermitQuery query = new McPermitQuery();
        query.setSysId(sysId);
        query.setRoleIds(roleIds);
        return this.listPermitLitByQuery(query);
    }

    @Override
    public List<PermitLitDO> listPermitLitByQuery(McPermitQuery query) {
        return permitDao.listPermitLitByQuery(query);
    }

    @Override
    public Integer countByQuery(McPermitQuery query) {
        return permitDao.countByQuery(query);
    }


    @Override
    public Integer updateByKey(McPermit permit) {
        if (permit.getUpdateTime() == null) {
            permit.setUpdateTime(DateUtil.getNow());
        }
        return permitDao.updateByKey(permit);
    }

    @Override
    public Integer updateStatus(Integer permitId, Integer status, String updater) {
        McPermit permit = new McPermit();
        permit.setPermitId(permitId);
        permit.setStatus(status);
        permit.setUpdater(updater);
        return this.updateStatus(permit);
    }

    @Override
    public Integer updateStatus(McPermit permit) {
        if (permit.getUpdateTime() == null) {
            permit.setUpdateTime(DateUtil.getNow());
        }
        return permitDao.updateByKey(permit);
    }

    @Override
    public Integer updateStatusBatch(List<Integer> permitIds, Integer status, String updater) {
        StatusDTO dto = new StatusDTO();
        dto.setIds(permitIds);
        dto.setStatus(status.intValue());
        dto.setUpdater(updater);
        return updateStatusBatch(dto);
    }

    @Override
    public Integer updateStatusBatch(StatusDTO dto) {
        if (dto.getUpdateTime() == null) {
            dto.setUpdateTime(DateUtil.getNow());
        }
        return permitDao.updateStatusBatch(dto);
    }

    @Override
    public Integer updateSort(Integer permitId, Integer sort) {
        McPermit permit = new McPermit();
        permit.setPermitId(permitId);
        permit.setSort(sort);
        return this.updateSort(permit);
    }

    @Override
    public Integer updateSort(McPermit permit) {
        return permitDao.updateSort(permit);
    }


    @Override
    public Integer insert(McPermit permit) {
        if (NumberUtil.isNullOrZero(permit.getParentId())) {
            permit.setParentId(GlobalConsts.TOP_ID);
        }
        permit.setSort(100);
        permit.setStatus(GlobalDict.STATUS_ENABLE);
        if (permit.getUpdateTime() == null) {
            permit.setUpdateTime(DateUtil.getNow());
        }
        permit.setCreater(permit.getUpdater());
        permit.setCreateTime(permit.getUpdateTime());
        return permitDao.insert(permit);
    }


    @Override
    public Integer deleteByKey(Integer permitId) {
        return permitDao.deleteByKey(permitId);
    }
}
